<!--
 * @Author: fange 653398363@qq.com
 * @Date: 2023-02-07 09:53:15
 * @LastEditors: fange 653398363@qq.com
 * @LastEditTime: 2023-02-13 10:27:11
 * @FilePath: \lfange.github.io\code\test.html
 * @Description: personal written
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <input
      type="text"
      name="ddd"
      id="ddd"
      onblur="console.log(11)"
      onchange="console.log(222)"
    />
  </head>

  <body>
    <script>
      function Change(e) {
        console.log('e', e)
      }

      function SuperType(name, age) {
        this.name = name
        this.age = age
      }
      SuperType.prototype.say = function () {
        return `name is ${this.name}, age is ${this.age}`
      }

      function Childs(name, age, color) {
        SuperType.call(this, name, age)
        const str = SuperType.prototype.say.call(this, name, age)
        console.log('Childs', str, this)
      }

      //过渡函数的原型继承父对象
      function Over() {}
      Over.prototype = SuperType.prototype
      Childs.prototype = new Over()

      let junjie = new Childs('junjie', 'not zero year old', 'greee')
      console.log('junjie', junjie.say())

      const downLoad = (url) => {
        return new Promise((resolve) => {
          resolve(`download url  resolved${url}`)
        })
      }

      const urls = new Array(12).fill(0).map((i, index) => {
        return { url: `url${index}` }
      })

      function multi(urls, maxLen) {
        const len = urls.length
        const res = []
        let sendCount = 0

        return new Promise((resolve, reject) => {
          while (sendCount < maxLen && sendCount < len) {
            next()
          }

          function next() {
            const cur = sendCount++
            if (res.length >= len) {
              resolve(res)
              return
            }

            const url = urls[cur].url

            downLoad(url).then((re) => {
              res.push(re)
              setTimeout(() => {
                if (cur < len) {
                  next()
                }
              }, 300)
            })
          }
        })
      }

      multi(urls, 3).then((res) => {
        // console.log('res =>', res)
      })
      // multiRequest(urls, 3).then((res) => {
      //   console.log('multiRequest =>', res)
      // })

      function multiRequest(urls, maxNum) {
        const len = urls.length // 请求总数量
        const res = new Array(len).fill(0) // 请求结果数组
        let sendCount = 0 // 已发送的请求数量
        let finishCount = 0 // 已完成的请求数量
        return new Promise((resolve, reject) => {
          // 首先发送 maxNum 个请求，注意：请求数可能小于 maxNum，所以也要满足条件2
          // 同步的 创建maxNum个next并行请求 然后才去执行异步的fetch 所以一上来就有5个next并行执行
          while (sendCount < maxNum && sendCount < len) {
            next()
          }
          function next() {
            let current = sendCount++ // 当前发送的请求数量，后加一 保存当前请求url的位置
            // 递归出口
            if (finishCount >= len) {
              // 如果所有请求完成，则解决掉 Promise，终止递归
              resolve(res)
              return
            }
            const url = urls[current]
            downLoad(url).then((result) => {
              finishCount++
              res[current] = result
              if (current < len) {
                // 如果请求没有发送完，继续发送请求
                next()
              }
            })
          }
        })
      }

      function getC(p = 'google') {
        let i = 0
        let j = 0
        console.log('str', str[0])
        const next = getNext(p)
        while (i < str.length && j < p.length) {
          if (j === 0 || str[i] === p[j]) {
            i++
            j++
          } else {
            j = 0
          }
          //    console.log('str', str[i])
        }
        console.log('j ', i, j)
        console.log(
          'i - p.length',
          i - p.length,
          str.substring([i - p.length], i)
        )
        if (j > p.length - 1) {
          return i - p.length
        } else {
          console.log('pppp', next)
          return 0
        }
        alert
      }
      function getNext(p) {
        const next = []
        let len = 0
        // next[0]的情况无法推出，手动初始化一下
        next[0] = 0
        // 通过循环推进i的下标
        for (let i = 0; i < p.length; i++) {
          // 对比当前公共前后缀的下一个字符
          // 当len = 0，或者 找到了匹配的公共前后缀的时候，退出循环
          while (len > 0 && p[i] !== p[len]) {
            // 进入循环，说明当前的最长公共前后缀的下一个字符不匹配
            // 就去找较短的最长公共前后缀
            // 较短公共前后缀必然是较长公共前后缀的公共前后缀
            // 通过next[next[i - 1] - 1],找到较短为的公共前后缀
            // len = next[i - 1]
            len = next[len - 1]
          }

          // p[i] === p[len]，表示找到了，在len的基础上 + 1就是next[i]的值
          if (p[i] === p[len]) len++
          // 如果 p[i] !== p[len]，表示没找到，此时len = 0，next[i] 也等于0
          next[i] = len
        }

        return next
      }

      // console.log('sssss', getC())
    </script>
  </body>
</html>
